package com.gxh.text.service.impl;


import com.gxh.text.entity.Tree;
import com.gxh.text.entity.TreseShop;

import com.gxh.text.mapper.TreeMapper;
import com.gxh.text.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class TreeServiceImpl  implements TreeService {


    @Autowired

    private TreeMapper tm;

//    @Override
//    public List findList(Goods goods) {
//        //获取所有根节点
//        List<Goods> list = tm.findlist(goods);
//        List<Goods> result  = new ArrayList<>();
//        for (Goods a : list){
//            if (a.getId() != 0){
//                TreseShop privilegeVO = getTree(a);
//               if (privilegeVO != null){
//                   result.add(privilegeVO);
//               }
//            }
//        }
//        return result;
//    }
//
//    /**
//     * 获取若有子节点
//     * @param a
//     * @return
//     */
//    private Goods getTree(Goods a) {
//        List<Goods> treelist = tm.selectByParentId(a.getId());
//        if (!treelist.isEmpty()){
//            a.setChildren(treelist);
//            for (Goods privilegeVO : treelist){
//                if (privilegeVO.getId() != 0){
//                    getTree(privilegeVO);
//                }
//            }
//        }
//        return  a;
//    }
@Override
public List SelectTree(TreseShop treseShop) {
    return tm.selectTrees(treseShop) ;
}
    @Override
    public List findList(Tree tree) {
        //查询菜单中的所有数据
       List<Tree> tlist = tm.selectTree(tree);
       //把查询到所有菜单list集合按照要求封装
        List<Tree>  resultlist = build(tlist);

        return resultlist;
    }



//    //把返回所有的菜单list集合进行方法封装
    private List<Tree> build(List<Tree> tlist) {
        //创建集合 用于数据最终封装
        List<Tree> findNode = new ArrayList<>();
        //把所有菜单list集合遍历，得到顶层菜单 pid=0菜单，设置level是1
        Integer num = 0;
        for (Tree treseShop : tlist) {
            //得到顶层菜单 pid=0菜单
          if (num.equals(treseShop.getShopId())){
              //设置顶层菜单的层级为1
              treseShop.setLevel(1);
               //根据顶层菜单，向里面进行查询子菜单，封装到finalNode里面
              findNode.add(selectChildren(treseShop,tlist));
          }
        }
        return  findNode;
    }
    //递归方法。因为向一层菜单里面放二层菜单，二层里面还要放三层.
    private Tree selectChildren(Tree treseShop, List<Tree> tlist) {
        //对象初始化
        treseShop.setChildren(new ArrayList<Tree>());
        //2 遍历所有菜单list集合，进行判断比较，比较id和pid值是否相同
        for (Tree tr : tlist){
            //判断 id和pid值是否相同

            if (Objects.equals(treseShop.getId(), tr.getShopId())){
                //菜单的level值+1
                int level = treseShop.getLevel() + 1;
                tr.setLevel(level);
                //如果children为空，进行初始化操作
                if (treseShop.getChildren() == null){
                    treseShop.setChildren(new ArrayList<Tree>());
                }
                //把查询出来的子菜单放到父菜单里面。这里再次调用自己
                treseShop.getChildren().add(selectChildren(tr,tlist));
            }
        }
        return  treseShop;
    }
}
